<?php
/**
 *  @author admin888 <doan.hut@gmail.com>
 *  Description : controller create thư mẫu 
 *  Date :   06/06/2011
 */ 
class ThumauController extends Zend_Controller_Action
{
    public $_iddn = 'null';
    
    /**
     * Deverloper : admin888 <doan.hut@gmail.com>
     * Desctiption : dung chung cho toan controler
     */ 
    public function init()
    {
        //view wellcome 
         $auth = Zend_Auth::getInstance();
         if($auth->hasIdentity()){
			$data = $auth->getIdentity();
			$this->view->name = $data->name;
			$this->view->iddangnhap = $data->uid;
			$this->_iddn = $data->uid;
			$cd = new Model_Caidat();
			$thongbao = $cd->getthongbao($data->uid);
			$this->view->thongbao = $thongbao;			

         }
        //phần lay out trên
	   $this->view->today =$date = date('m-d-Y H:i:s');
       $reponse = $this->getResponse();
       $reponse->insert('top', $this->view->render('index/top.phtml'));
       $this->view->title = " Quản lý thư mẫu - Email Manager ";  
    }
    /**
     * Derverloper : admin888 <doan.hut@gmail.com>
     * Description : Check dang nhập  
     */ 
	public function preDispatch(){
	    $auth = Zend_Auth::getInstance();
	    if (!$auth->hasIdentity()) {
	        if ($this->_request->getActionName() != 'login') {
	            $this->_redirect('login');
	        }
         
	    }
	}
    /**
     * Derverloper : admin888 <doan.hut@gmail.com>
     * Description : Action Index 
     * 
     */
     public function indexAction()
     {
         $sl = new Model_Thumau();
         $get = $this->_getAllParams();
         if (isset($get['keytimkiem'])){
      	   $tukhoa = trim($get['keytimkiem']);
    	 }
    	 if (isset($get['chungloaithu'])){
    	   $cltm = trim($get['chungloaithu']);
    	 }
    	 if ($tukhoa == '' && $cltm == ''){
    	 	// lay toan bo thu mau
         	$data = $sl->getallthumau($this->_iddn);
    	 }else if($tukhoa != '' &&  $cltm == ''){
    	 	//tim thu theo tu khoa
    	 	$data = $sl->getallthumautheotukhoa($this->_iddn,$tukhoa);
    	 }else if($cltm != '' &&  $tukhoa == '') {
    	 	// tim thu theo  chung loai thu mau
    	 	$data = $sl->getallthumautheocltm($this->_iddn,$cltm);
    	 }else {
    	 	// tim theo ca tu khoa va loai thu mau
    	 	$data = $sl->getallthumautheocltmvatukhoa($this->_iddn,$cltm,$tukhoa);
    	 }
		// phan trang 
		$page = $this->_getParam('page',1); // bắt biến trên trang
		$paginator = Zend_Paginator::factory($data); // gán mảng vào zend_paginator
		$paginator->setCurrentPageNumber($page); // chuyển tới trang
		$paginator->setItemCountPerPage(15); // số bản ghi trên 1 trang
		$this->view->paginator = $paginator; // view số trang 
		// end phân trang    	 
    	 
         $this->view->thumau = $paginator;	
        //lay danh muc thu mau 
     	$dmtm = $sl->getdmthumau();
        $this->view->dmtm = $dmtm;
     }
     /**
      * admin888 <doan.hut@gmail.com>
      * Desscritption : Tạo mới thư mẫu 
      * 
      */ 
     public function themthumauAction()
     {
        $addthumau = new Model_Thumau();
        $dmtm = $addthumau->getdmthumau();
        $this->view->dmtm = $dmtm;
     }
     /**
      * admim888 <doan.hut@gmail.com>
      * thêm thư mẫu và db
      * 
      */
     public function addthumauAction()
     {
        $addthu = new Model_Thumau();
        $data = $this->_getAllParams();
        if($data['tenthumau'] != ""){
            $add = $addthu->themthumau($data);
             $this->_redirect('thumau/index');
        }else{
            $this->_redirect('index');
        }
        $this->getHelper('viewRenderer')->setNoRender(); // set khong su dung template
     }
     /**
      * admin888 <doan.hut@gmail.com>
      * Enter description :lay thu mau theo id 
      */
     public function suathumauAction()
     {
     	$sl= new Model_Thumau();
     	$idtm = $this->_getParam('idtm');
     	$data = $sl->getthumau($idtm);
     	$this->view->thumausua = $data;
     	//lay danh muc thu mau 
     	$dmtm = $sl->getdmthumau();
        $this->view->dmtm = $dmtm;
     }
     /**
      * admin888 <doan.hut@gmail.com>
      * Description : update thu mẫu
      */
     public function updatethumauAction()
     {
     	$update = new Model_Thumau();
     	$data = $this->_getAllParams();
     	$ud = $update->updatethumau($data);
     	if($ud){
     		$this->_redirect('thumau');
     	}else{
     		$this->_redirect('index');
     	}
     	$this->getHelper('viewRenderer')->setNoRender(); // set khong su dung template
     }

     
       
    
}
 ?>